package com.common.buttons
{
	import com.common.displayObjects.Component;
	import com.common.displayObjects.ComponentChildren;
	
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	public class Button extends Component
	{
		private var bmNormal:BitmapData;
		private var bmOver:BitmapData;
		private var bmDown:BitmapData;
		public var selectable:Boolean;
		private var txtLable:TextField;
		private var lable:String;
		
		public function Button(lable:String='', selectable:Boolean = false)
		{
			this.lable = lable;
			this.selectable = selectable;
			super();
			buttonMode = true;
			createChildren();
			addEventListener(MouseEvent.MOUSE_OVER, onMouse);
			addEventListener(MouseEvent.MOUSE_DOWN, onMouse);
			addEventListener(MouseEvent.MOUSE_OUT, onMouse);
		}
		
		protected function onMouse(event:MouseEvent):void
		{
			switch(event.type)
			{
				case MouseEvent.MOUSE_OVER:
					bmd = bmOver;
					break;
				case MouseEvent.MOUSE_DOWN:
					bmd = bmDown;
					break;
				case MouseEvent.MOUSE_OUT:
					if (!selectable)bmd = bmNormal;
					else bmd = null;
					break;
			}
		}
		
		private var g:Graphics;
		override protected function createChildren():void
		{
			super.createChildren();
			g = graphics;
			g.beginFill(0xff0000);
			g.drawRoundRect(0, 0, 88, 22, 3, 3);
			g.beginFill(0x33411f);
			g.drawRoundRect(1, 1, 86, 20, 3, 3);
			g.endFill();
			
			txtLable = new TextField();
			txtLable.width = width;
			txtLable.height = height;
			txtLable.text = lable;
			txtLable.autoSize = TextFieldAutoSize.CENTER;
			txtLable.mouseEnabled = false;
			txtLable.selectable = false;
			addChild(txtLable);
		}		
		
		private var bmd:BitmapData;
		private function draw():void
		{
			if (!bmd)return;
			g = graphics;
			g.beginBitmapFill(bmd, null, false);
			g.drawRect(0, 0, bmd.width, bmd.height);
			g.endFill();
		}
		
		override public function dispose():void
		{
			super.dispose();
			bmNormal = null;
			bmDown = null;
			bmOver = null;
			g.clear();
		}
	}
}